<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-13
 * Time: 18:04
 */
namespace app\merchant\service;

use app\common\service\Service;
use think\Db;
use think\Loader;
use think\Model;

class Merchant extends Service
{
    protected $merchant_model = null;
    public function __construct(){
        parent::__construct();
        $this->merchant_model = new \app\merchant\model\Merchant();
    }

    public function check_login() {
        $merId = intval(session("mer_id"));
        if($merId) {
            $merchant = $this->merchant_model->getMerchantById($merId);
            if($merchant) {
                if($merchant['status'] != 1) {
                    switch ($merchant['status']) {
                        case '0':
                            return $this->ret
                                ->setCode(\app\merchant\define\Merchant::$Code_Mer_Account_Closure)
                                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Account_Closure);
                        default:
                            return $this->ret
                                ->setCode(\app\merchant\define\Merchant::$Code_Mer_Account_Error)
                                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Account_Error);

                    }
                }else{
                    unset($merchant['password']);
                    return $this->ret
                        ->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                        ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success)
                        ->setData("merchant", $merchant);
                }
            }else {
                return $this->ret
                    ->setCode(\app\merchant\define\Merchant::$Code_Mer_No_Login)
                    ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_No_Login);
            }
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_No_Login)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_No_Login);
        }
    }

    public function check_auth($merchant, $rule) {
        if($this->merchant_model->hasAuth($merchant['id'], $rule)) {
            return $this->ret
                ->setCode(\app\merchant\define\Merchant::$Code_Mer_Success);
        }else {
            return $this->ret
                ->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_No_Auth);
        }
    }

    public function get_mer_cert($merchant) {
        $cert = $this->merchant_model->getMerCert($merchant['id']);
        if($cert) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("cert",$cert);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error);
        }
    }

    public function edit_mer_cert($_form,$mer_id){
        $cert = $this->merchant_model->getMerCert($mer_id);
        $ret = false;
        if(!$cert) {
            $_form['mer_id'] = $mer_id;
            $ret = $this->merchant_model->insertMerCert($_form);
        }else {
            $_form['valid'] = 0;
            $ret = $this->merchant_model->saveMerCert($_form,$cert['id']);
        }
        if($ret) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Mer_Mer_Waitting_Audit);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Error);
        }
    }

    public function get_mer_deposit($merchant) {
        $deposit = $this->merchant_model->getMerDeposit($merchant['id']);
        if($deposit) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("deposit",$deposit);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error);
        }
    }

    public function get_mer_info($merchant) {
        $info = $this->merchant_model->getMerInfo($merchant['id']);
        if($info) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("info", $info);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error);
        }
    }
    public function edit_mer_info($_post) {
        $info = $this->merchant_model->getMerInfo($_post['mer_id']);
        $ret = false;
        if($info) {
            $ret = $this->merchant_model->saveMerInfo($_post,$info['id']);
        } else {
            $ret = $this->merchant_model->insertMerInfo($_post);
        }
        if($ret) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success);
        } else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
        }
    }

    public function get_mer_pay($merchant) {
        $pay = $this->merchant_model->getMerPay($merchant['id']);
        if($pay) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("pay", $pay);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error);
        }
    }

    public function get_mer_store($merchant) {
        $store = $this->merchant_model->getMerStore($merchant['id']);
        if($store) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("store", $store);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error);
        }
    }

    public function edit_mer_store($_form,$mer_id) {
        $store = $this->merchant_model->getMerStore($mer_id);
        $ret = null;
        if($store) {
            $_form['valid'] = 0;
            $ret = $this->merchant_model->saveMerStore($_form,$store['id']);
        }else {
            $_form['mer_id'] = $mer_id;
            $ret = $this->merchant_model->insertMerStore($_form);
        }
        if($ret) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success);
        }
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
            ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
    }

    public function login($_form) {
        $check = $this->merchant_model->checkForm($_form, 'login');
        if($check) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg($check);
        }
        if(okay_isemail($_form['username'])) {
            $mer = $this->merchant_model->getMerchantBy("email", $_form['username']);
        }else if(okay_ismobile($_form['username'])) {
            $mer = $this->merchant_model->getMerchantBy("mobile", $_form['username']);
        }else {
            $mer = $this->merchant_model->getMerchantBy("username", $_form['username']);
        }
        if(!$mer) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Account_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Account_Error);
        }
        $confirm_pwd = inge_encryption_pwd($_form['password'],'Merchant');
        if($confirm_pwd == $mer['password']) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Login)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Login)
                ->setData("merchant", $mer);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Login_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Login_Error);
        }
    }

    public function register($_form) {
        $check = $this->merchant_model->checkForm($_form, 'register');
        if($check) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg($check);
        }
        $_form["password"] = inge_encryption_pwd($_form['password'],'Merchant');
        $_form['reg_time'] = time();
        $_form['reg_ip'] = ip2long(inge_get_ip());
        $resId = $this->merchant_model->insertGetId($_form);
        Db::name("MerAuthAso")->insert(['auth_id' => 1,"mer_id" => $resId]);
        if($resId) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Register_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Register_Success)
                ->setData("mer_id",$resId);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Register_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Register_Error);
        }
    }

}